package edu.nf.hotel.userdao.impl;

import edu.nf.hotel.user.User;
import edu.nf.hotel.userdao.UserDao;
import edu.nf.hotel.util.ConnUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-06-09
 * @Author 陈
 * 用户实现dao类
 */
public class UserDaoImpl implements UserDao {
    /**
     * 根据用户名登录
     * @param userName
     * @return
     */
    @Override
    public User select(String userName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select id, username, passwords, sex, phone,level,email , times  from user_info where username=?" ;
        BeanHandler<User> handler =new BeanHandler<>(User.class);
        User user = executor.executeQuery(sql,handler,userName);
        return user;
    }

    /**
     * 根据id查询会员信息
     * @param id
     * @return
     */
    @Override
    public User select(int id) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select id, username, passwords, sex, phone,level,email , times  from user_info where id=?";
        BeanHandler<User> handler = new BeanHandler<>(User.class);
        User user = executor.executeQuery(sql, handler, id);
        System.out.println(user.getUserName());
        System.out.println(user.getSex());
        System.out.println(user.getPhone());
        System.out.println(user.getLevel());
        System.out.println(user.getEmail());
        System.out.println(user.getTime());
        return user;
    }

    /**
     * 查询全部会员
     * @return
     */
    @Override
    public List<User> selectUser() {
        SqlExecutor sqlExecutor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<User> handler = new BeanListHandler<>(User.class);
        String sql = "select id, username, passwords, sex, phone,level,email , times,address  from user_info";
        List<User> list = sqlExecutor.executeQuery(sql,handler);
        list.forEach(user -> System.out.println(user.getUserName()));
        return list;
    }


    /**
     * 根据ID删除会员
     *
     * @param id
     * @return
     */
    @Override
    public void delete(int id) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "delete from user_info where id = ?";
        executor.executeUpdate(sql, id);
    }

    /**
     * 添加会员信息
     * @param user
     */
    @Override
    public void insert(User user) {
        SqlExecutor sqlExecutor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "insert into user_info(username, passwords, sex, phone, email,address) values(?,?,?,?,?,?)";
        sqlExecutor.executeUpdate(sql,user.getUserName(),user.getPassword(),user.getSex(),user.getPhone(),user.getEmail(),user.getAddress());
    }

    /**
     * 修改数据
     *
     * @param user
     * @return
     */
    @Override
    public int update(User user) {
        SqlExecutor sqlExecutor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "update user_info set username=?,passwords=?,sex=?,phone=?,address=? where id=?";
        return sqlExecutor.executeUpdate(sql,user.getUserName(),user.getPassword(),user.getSex(),user.getPhone(),user.getAddress(),user.getId());
    }

}
